//
//  CZEmoticonToolBar.swift
//  表情键盘新工程-74.准备
//
//  Created by itcast on 16/5/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

/// 1.定义协议 <NSObject>
protocol CZEmoticonToolBarDelegate: NSObjectProtocol {
    // 2.定义协议方法
    func didSelectButtonType(didSelectedType: CZEmoticonToolBarType)
}

enum CZEmoticonToolBarType: Int {
    case Recent     // 最近
    case Default    // 默认
    case Emoji      // emoji
    case Lxh        // 浪小花
}

/// 表情键盘toolBar
/// 父控件用autolayout,子控件是用frame, 注意不要一个View顶部使用autolayout左边使用frame
class CZEmoticonToolBar: UIView {
    
    // MARK: - 属性
    // 3.定义代理属性
    weak var delegate: CZEmoticonToolBarDelegate?
    
    /// 记录当前哪个按钮选中了
    private var selectedButton: UIButton?
    
    /// 按钮数组
    private var buttons = [UIButton]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    private func prepareUI() {
        backgroundColor = UIColor.lightGrayColor()
        
        // 创建4个按钮
        let titles = ["最近", "默认", "emoji", "浪小花"]
        var index = 0
        for title in titles {
            // 创建按钮
            let button = UIButton()
            
            // 设置
            button.setTitle(title, forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_normal"), forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_selected"), forState: UIControlState.Highlighted)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_selected"), forState: UIControlState.Selected)
            
            button.tag = index
            // 按钮添加点击事件
            button.addTarget(self, action: "didClickButton:", forControlEvents: UIControlEvents.TouchUpInside)
            
            // 添加到父控件
            self.addSubview(button)
            
            // 添加到数组中
            buttons.append(button)
            
            index++
        }
        
        // 默认选中第最近按钮
        let defaultButton = buttons[0]
        switchSelectedButton(defaultButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 计算一个按钮的宽度
        let buttonWidth = self.frame.width / CGFloat(buttons.count)
        // 设置按钮的frame
        var index = 0
        for button in buttons {
            
            let frame = CGRect(x: CGFloat(index) * buttonWidth, y: 0, width: buttonWidth, height: self.frame.height)
            button.frame = frame
            
            index++
        }
    }
    
    // MARK: - 点击事件
    @objc private func didClickButton(button: UIButton) {
        let type = CZEmoticonToolBarType(rawValue: button.tag)!
        
        // 选中点击的按钮
        switchSelectedButton(button)
        
        // 告诉外面的对象选中了一个按钮
        // 4.调用代理方法
        delegate?.didSelectButtonType(type)
    }
    
    // MARK: - 公开方法
    /**
    设置按钮选中
    
    - parameter section: 通过对应的组
    */
    func switchSelectedButtonWitchSection(section: Int) {
        // 获取section对应的按钮
        let button = buttons[section]
        
        switchSelectedButton(button)
    }
    
    /**
     切换选中按钮
     
     - parameter button: 要选中的按钮
     */
    private func switchSelectedButton(button: UIButton) {
        // 0.如果要选中的button已经选中了,不需要再去选中了
        if button == selectedButton {
            return
        }
        
        // 1.将之前的选中按钮设置为不选中
        selectedButton?.selected = false
        
        // 2.设置传入按钮为选中状态
        button.selected = true
        
        // 3.记录下传入的按钮为选中的按钮
        selectedButton = button
    }
}
